﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;

namespace AutoParkingManagement
{
    public partial class TransferWorking : Form
    {
        public Guid EmployeeId { get; set; }

        public TransferWorking()
        {
            InitializeComponent();
        }

        public TransferWorking(Guid employeeId)
        {
            InitializeComponent();
            this.EmployeeId = employeeId;
        }

        private DataTable GetDataSource(DateTime dtFrom)
        {
            string sql = @"SELECT TicketType, 
                            SUM(Quantity) AS Quantity, 
                            SUM(Amount) AS Amount
                            FROM
                            (
	                            SELECT N'Xe trả trước' TicketType, COUNT([ID]) AS Quantity      
		                              ,0 AS Amount
	                              FROM [AutoParking].[dbo].[EntryExit]
	                              WHERE ExitTime IS NOT NULL
		                            AND ExitUpdateUser=@ExitUpdateUser 
		                            AND ExitTime >= @From
	                              AND PlateNumber IN (SELECT PlateNumber FROM MonthCard)
	                              AND Price=0
	                            UNION
	                            SELECT CardType AS TicketType, COUNT([ID]) AS Quantity      
		                              ,SUM(Price) AS Amount
	                              FROM [EntryExit] e
	                              WHERE ExitTime IS NOT NULL
		                            AND ExitUpdateUser=@ExitUpdateUser 
		                            AND ExitTime >= @From
	                              AND PlateNumber NOT IN (SELECT PlateNumber FROM MonthCard)
	                              GROUP BY CardType
	                             UNION
	                             SELECT CardType AS TicketType, COUNT([ID]) AS Quantity      
		                              ,SUM(Price) AS Amount
	                              FROM [EntryExit] e
	                              WHERE ExitTime IS NOT NULL
		                            AND ExitUpdateUser=@ExitUpdateUser 
		                            AND ExitTime >= @From
	                              AND PlateNumber IN (SELECT PlateNumber FROM MonthCard)
	                              AND Price>0
	                              GROUP BY CardType
                            )tmp
                            GROUP BY TicketType";
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter("ExitUpdateUser", SqlDbType.UniqueIdentifier);
            parms[0].Value = MainParent.EmpWorking.Employee.Id;

            parms[1] = new SqlParameter("From", SqlDbType.DateTime);
            parms[1].Value = dtFrom;

            SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.Text, sql, parms);
            DataTable dt = new DataTable();
            dt.Load(reader);          
            
            return dt;
        }

        private DateTime GetLoginTime()
        {
            string sql = @"SELECT MAX([LoginTime])      
                          FROM [Working]  
                          WHERE EmployeeID=@EmployeeID AND[LogoutTime] IS NULL";
            SqlParameter parm = new SqlParameter("EmployeeId", SqlDbType.UniqueIdentifier);
            parm.Value = MainParent.EmpWorking.Employee.Id;
            object obj = SqlHelper.ExecuteScalar(CommandType.Text, sql, parm);
            if (obj == null) return DateTime.Now;
            return Convert.ToDateTime(obj);
        }

        private string  GetFullName()
        {
            string sql = @"SELECT FirstName + ' '+ LastName
                        FROM Employee
                        WHERE UserName=@UserName";            
            SqlParameter parm = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
            parm.Value = Login.Username;
            object obj = SqlHelper.ExecuteScalar(CommandType.Text, sql, parm);
            if (obj == null) return String.Empty;
            return obj.ToString();
        }

        private void TransferWorking_Load(object sender, EventArgs e)
        {
            try
            {
                DateTime loginTime = GetLoginTime();
                DateTime reportTime = SqlHelper.SystemDate;
                DataTable dt = GetDataSource(loginTime);                
                ReportParameter[] param = new ReportParameter[5];
                string format = "dd/MM/yyyy HH:mm";                
                param[0] = new ReportParameter("ParkingFloor", AppConfig.ParkingFloor);
                param[1] = new ReportParameter("ReportDate", reportTime.ToString(format));
                param[2] = new ReportParameter("EmployeeName", GetFullName());
                param[3] = new ReportParameter("InTime", loginTime.ToString(format));
                param[4] = new ReportParameter("OutTime", reportTime.ToString(format));
                reportViewer1.LocalReport.SetParameters(param);
                //prepare report data source
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "DataSet1_TransferShift";
                //rds.Value = ds.Tables[0];
                rds.Value = dt;
                reportViewer1.LocalReport.DataSources.Add(rds);
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xuất báo cáo.");
            }
            this.reportViewer1.RefreshReport();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
